unit clsPos_Venda;

interface

uses Dm, dbtables, biblio, sysutils, menu;

type
  TPosVenda = class(TObject)
  private
    PosV_Codigo: string;
    Ent_Codigo: string;
  public
    Nota_Numero: string;
    PosV_Data: TDateTime;
    PosV_Contato: string;
    PosV_Obs1: string;
    PosV_Obs2: string;
    function Gravar(p_Cod, p_Ent: string): Byte;
    function Recuperar(p_Cod, p_Ent: string): Byte;
    function ExcluirTudo(p_Ent_Codigo: string): Byte;
    function Excluir(p_Cod, p_Ent: string): Byte;
    procedure Consultar(var p_q: TQuery; p_Ent: string);
    function Ultimo(p_Ent: string): string;
    function RetornaData(p_Tipo: Byte; p_Ent: string): TDateTime;
  end;

implementation

{ TPosVenda }

procedure TPosVenda.Consultar(var p_q: TQuery; p_Ent: string);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Pos_Venda where Ent_Codigo=:k_Ent_Codigo ' +
    'order by PosV_Codigo');
  p_q.parambyname('k_Ent_Codigo').asstring := strzero(p_Ent, 5);
  p_q.Open;
end;

function TPosVenda.Excluir(p_Cod, p_Ent: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Pos_Venda where ' +
    'PosV_Codigo =:Cod and Ent_Codigo =:Ent');
  tq.ParamByName('Cod').AsString := StrZero(p_Cod, 3);
  tq.ParamByName('Ent').AsString := StrZero(p_Ent, 5);
  try
    tq.ExecSQL;
    Result := 1;
  except
    Result := 0;
  end;
  tq.Close;
  tq.Free;
end;

function TPosVenda.ExcluirTudo(p_Ent_Codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Pos_Venda where ' +
    'Ent_Codigo =:Ent');
  tq.ParamByName('Ent').AsString := StrZero(p_Ent_Codigo, 5);
  try
    tq.ExecSQL;
    Result := 1;
  except
    Result := 0;
  end;
  tq.Close;
  tq.Free;
end;

function TPosVenda.Gravar(p_Cod, p_Ent: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Pos_Venda where PosV_Codigo =:Cod and Ent_Codigo =:Ent');
  tq.ParamByName('Cod').AsString := StrZero(p_Cod, 3);
  tq.ParamByName('Ent').AsString := StrZero(p_Ent, 5);
  tq.Open;
  if tq.Eof then begin
    Result := 0;
    tq.active := False;
    tq.close;
    tq.sql.Clear;
    tq.params.clear;
    tq.sql.add('Insert into Pos_Venda values (:a,:b,:c,:d,:e,:f,:g)');
  end else begin
    Result := 1;
    tq.sql.add('Update Pos_venda set Nota_Numero =:c, PosV_Data =:d, ' +
      'PosV_Contato =:e, PosV_Obs1 =:f, PosV_Obs2 ' +
      'where PosV_Codigo =:a and Ent_Codigo =:b');
  end;
  tq.ParamByName('a').AsString := p_Cod;
  tq.ParamByName('b').AsString := p_Ent;
  tq.ParamByName('c').AsString := Nota_Numero;
  tq.ParamByName('d').AsDateTime := PosV_Data;
  tq.ParamByName('e').AsString := PosV_Contato;
  tq.ParamByName('f').AsString := PosV_Obs1;
  tq.ParamByName('g').AsString := PosV_Obs2;
  try
    tq.ExecSQL;
  except
  end;
  tq.Close;
  tq.Free;
end;

function TPosVenda.Recuperar(p_Cod, p_Ent: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Pos_Venda where PosV_Codigo =:Cod and Ent_Codigo =:Ent');
  tq.ParamByName('Cod').AsString := StrZero(p_Cod, 3);
  tq.ParamByName('Ent').AsString := StrZero(p_Ent, 5);
  tq.Open;
  if tq.Eof then begin
    Result := 0;
    PosV_Codigo := p_Cod;
    Ent_Codigo := p_Ent;
    Nota_Numero := '';
    PosV_Data := 0;
    PosV_Contato := '';
    PosV_Obs1 := '';
    PosV_Obs2 := '';
  end else begin
    Result := 0;
    PosV_Codigo := p_Cod;
    Ent_Codigo := p_Ent;
    Nota_Numero := tq.FieldByName('Nota_Numero').AsString;
    PosV_Data := tq.FieldByName('PosV_Date').AsDateTime;
    PosV_Contato := tq.FieldByName('PosV_Contato').AsString;
    PosV_Obs1 := tq.FieldByName('PosV_Obs1').AsString;
    PosV_Obs2 := tq.FieldByName('PosV_Obs2').AsString;
  end;
  tq.Close;
  tq.Free;
end;

function TPosVenda.RetornaData(p_Tipo: Byte; p_Ent: string): TDateTime;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select max(Posv_Data) Ult, min(Posv_Data) Pri ' +
    'from Pos_Venda where Ent_Codigo =:Ent');
  tq.Params[0].AsString := p_ent;
  tq.Open;
  Result := 0;
  if not tq.Eof then begin
    if p_Tipo = 0 then
      Result := tq.FieldByName('Pri').AsDateTime
    else
      Result := tq.FieldByName('Ult').AsDateTime;
  end;
  tq.Close;
  tq.Free;
end;

function TPosVenda.Ultimo(p_Ent: string): string;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select max(PosV_Codigo) Ultimo ' +
    'from Pos_Venda ' +
    'where Ent_Codigo =:Ent');
  tq.ParamByName('Ent').AsString := StrZero(p_Ent, 5);
  tq.Open;
  if tq.FieldByName('Ultimo').IsNull then
    Result := StrZero(1, 3)
  else
    Result := IntToStr(tq.FieldByName('Ultimo').AsInteger + 1);
  tq.Close;
  tq.Free;
end;

end.
